
#import "SLUIKit.h"

UIViewController * getRootViewController(){
	return [[[UIApplication sharedApplication] keyWindow] rootViewController];
}

void showAlert(NSString *title, NSString *message){
	UIAlertView *view = [[UIAlertView alloc] initWithTitle:title 
												   message:message 
												  delegate:nil 
										 cancelButtonTitle:NSLocalizedString(@"OK", @"General") 
										 otherButtonTitles:nil];
	[view show];
	[view release];
}

UIImage * imageInRect(UIImage *image, CGRect rect){
	CGImageRef imgref = CGImageCreateWithImageInRect([image CGImage], rect);
	return [UIImage imageWithCGImage:imgref];
}

UIImage * resizeImageToSize(UIImage *orgImg, CGSize size){
	UIGraphicsBeginImageContext(size);
	[orgImg drawInRect:CGRectMake(0, 0, size.width, size.height)];
	UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	return resizedImage;
}

UIImage * stretchImageWithFixedPoints(UIImage *sourceImage, CGSize targetSize, float wl, float wr, float ht, float hb){
	CGSize sourceSize = [sourceImage size];
	
	CGRect ltR = CGRectMake(0, 0, wl, ht);
	CGRect rtR = CGRectMake(sourceSize.width - wr, 0, wr, ht);
	CGRect lbR = CGRectMake(0, sourceSize.height - hb, wl, hb);
	CGRect rbR = CGRectMake(sourceSize.width - wr, sourceSize.height - hb, wr, hb);
	CGRect centerR = CGRectMake(wl, ht, sourceSize.width - wl - wr, sourceSize.height - ht - hb);
	
	CGRect ctR = CGRectMake(centerR.origin.x, ltR.origin.y, centerR.size.width, ltR.size.height);
	CGRect rmR = CGRectMake(rtR.origin.x, centerR.origin.y, rtR.size.width, centerR.size.height);
	CGRect cbR = CGRectMake(centerR.origin.x, lbR.origin.y, centerR.size.width, lbR.size.height);
	CGRect lmR = CGRectMake(ltR.origin.x, centerR.origin.y, ltR.size.width, centerR.size.height);
	
	UIImage *lt = imageInRect(sourceImage, ltR);
	UIImage *rt = imageInRect(sourceImage, rtR);
	UIImage *lb = imageInRect(sourceImage, lbR);
	UIImage *rb = imageInRect(sourceImage, rbR);
	UIImage *center = imageInRect(sourceImage, centerR);
	
	UIImage *ct = imageInRect(sourceImage, ctR);
	UIImage *rm = imageInRect(sourceImage, rmR);
	UIImage *cb = imageInRect(sourceImage, cbR);
	UIImage *lm = imageInRect(sourceImage, lmR);
	
	CGRect fixedLtR = ltR;
	CGRect fixedRtR = CGRectMake(targetSize.width - rtR.size.width, 0, rtR.size.width, rtR.size.height);
	CGRect fixedLbR = CGRectMake(0, targetSize.height - lbR.size.height, 
								 lbR.size.width, lbR.size.height);
	CGRect fixedRbR = CGRectMake(targetSize.width - rbR.size.width, targetSize.height - rbR.size.height, 
								 rbR.size.width, rbR.size.height);
	
	CGRect fixedCenterR = CGRectMake(wl, ht, targetSize.width - wl - wr, targetSize.height - ht - hb);
	
	CGRect fixedCtR = CGRectMake(wl, 0, fixedCenterR.size.width, ht);
	CGRect fixedRmR = CGRectMake(targetSize.width - wr, ht, wr, fixedCenterR.size.height);
	CGRect fixedCbR = CGRectMake(wl, targetSize.height - hb, fixedCenterR.size.width, hb);
	CGRect fixedLmR = CGRectMake(0, ht, wl, fixedCenterR.size.height);
	
	UIGraphicsBeginImageContext(targetSize);
	[lt drawInRect:fixedLtR];
	[rt drawInRect:fixedRtR];
	[lb drawInRect:fixedLbR];
	[rb drawInRect:fixedRbR];
	[center drawInRect:fixedCenterR];
	[ct drawInRect:fixedCtR];
	[rm drawInRect:fixedRmR];
	[cb drawInRect:fixedCbR];
	[lm drawInRect:fixedLmR];
	UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
	UIGraphicsEndImageContext();
	
	return resizedImage;
}


